import { defineStore } from 'pinia'
import { ref } from 'vue'
import { 
  getAnswerList, 
  createAnswer, 
  updateAnswer, 
  deleteAnswer, 
  acceptAnswer, 
  voteAnswer, 
  cancelVote 
} from '../api/answer'

export const useAnswerStore = defineStore('answer', () => {
  // 回答列表
  const answerList = ref([])
  // 列表分页
  const pagination = ref({
    current: 1,
    size: 10,
    total: 0
  })
  // 加载状态
  const loading = ref(false)
  
  // 获取回答列表
  const fetchAnswerList = async (questionId, params) => {
    try {
      loading.value = true
      
      // 分页参数
      const queryParams = {
        current: pagination.value.current,
        size: pagination.value.size,
        ...params
      }
      
      const res = await getAnswerList(questionId, queryParams)
      
      answerList.value = res.data.records
      pagination.value.current = res.data.current
      pagination.value.size = res.data.size
      pagination.value.total = res.data.total
      
      return res.data
    } finally {
      loading.value = false
    }
  }
  
  // 创建回答
  const submitAnswer = async (answerData) => {
    try {
      loading.value = true
      const res = await createAnswer(answerData)
      
      // 添加到回答列表
      if (answerList.value.length > 0 && answerList.value[0].questionId === answerData.questionId) {
        answerList.value.unshift(res.data)
        pagination.value.total += 1
      }
      
      return res.data
    } finally {
      loading.value = false
    }
  }
  
  // 更新回答
  const modifyAnswer = async (answerData) => {
    try {
      loading.value = true
      const res = await updateAnswer(answerData)
      
      // 更新列表中的回答
      const index = answerList.value.findIndex(item => item.id === answerData.id)
      if (index !== -1) {
        answerList.value[index] = res.data
      }
      
      return res.data
    } finally {
      loading.value = false
    }
  }
  
  // 删除回答
  const removeAnswer = async (id) => {
    try {
      loading.value = true
      await deleteAnswer(id)
      
      // 从列表中删除
      const index = answerList.value.findIndex(item => item.id === id)
      if (index !== -1) {
        answerList.value.splice(index, 1)
        pagination.value.total -= 1
      }
    } finally {
      loading.value = false
    }
  }
  
  // 采纳回答
  const acceptAnswerAction = async (id) => {
    try {
      loading.value = true
      await acceptAnswer(id)
      
      // 更新列表中的回答状态
      answerList.value.forEach(item => {
        item.isAccepted = item.id === id ? 1 : 0
      })
    } finally {
      loading.value = false
    }
  }
  
  // 点赞回答
  const voteAnswerAction = async (id) => {
    try {
      loading.value = true
      await voteAnswer(id)
      
      // 更新列表中的回答点赞状态
      const answer = answerList.value.find(item => item.id === id)
      if (answer) {
        answer.voteCount += 1
        answer.hasVoted = true
      }
    } finally {
      loading.value = false
    }
  }
  
  // 取消点赞
  const cancelVoteAction = async (id) => {
    try {
      loading.value = true
      await cancelVote(id)
      
      // 更新列表中的回答点赞状态
      const answer = answerList.value.find(item => item.id === id)
      if (answer) {
        answer.voteCount -= 1
        answer.hasVoted = false
      }
    } finally {
      loading.value = false
    }
  }
  
  return {
    answerList,
    pagination,
    loading,
    fetchAnswerList,
    submitAnswer,
    modifyAnswer,
    removeAnswer,
    acceptAnswerAction,
    voteAnswerAction,
    cancelVoteAction
  }
}) 